package com.software.controller;


import com.software.base.ResultInfo;
import com.software.bean.CartBean;
import com.software.dto.AddCartDto;
import com.software.dto.DeleteCartDto;
import com.software.service.CartService;
import com.software.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2024-04-10
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * @description:加入购物车
     * @author: wyj
     * @date: 2024/4/10 12:55
     * @param:
     * @return:
     */
    @PostMapping("/front/addCart")
    @ResponseBody
    public ResultInfo addCart(@RequestBody AddCartDto addCartDto) {
        Integer result = cartService.addCart(addCartDto);
        AssertUtil.isTrue(result != 1, "加入购物车失败");
        return ResultInfo.success();
    }

    /**
     * @description:获取购物车商品信息列表
     * @author: wyj
     * @date: 2024/4/10 18:45
     * @param:
     * @return:
     */
    @GetMapping("/front/getCartList/{customerId}")
    @ResponseBody
    public ResultInfo getCartList(@PathVariable Integer customerId) {
        List<CartBean> cartList = cartService.getCartList(customerId);
        return ResultInfo.success(cartList);
    }

    /**
     * @description:删除购物车商品
     * @author: wyj
     * @date: 2024/4/10 18:45
     * @param:
     * @return:
     */
    @PostMapping("/front/deleteCartProductById")
    @ResponseBody
    public ResultInfo deleteCartProductById(@RequestBody DeleteCartDto DeleteCartDto) {
        System.out.println(DeleteCartDto);
        Integer result = cartService.deleteCartProductById(DeleteCartDto);
        AssertUtil.isTrue(result != 1, "删除失败");
        return ResultInfo.success();
    }
}
